package poker;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ListIterator;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;


public class HoofdSchermGUI extends JFrame
{
	
	private static final long serialVersionUID = 1L;
	private MenuGUI menu;
	public PokerTafelGUI tafel;
	SpelRonde spelronde;
	Speler speler;
	private JFrame jf;
	JTextField ip, port;
	JLabel statusBar;
	Image backSideCard = Toolkit.getDefaultToolkit().getImage("img\\beach.jpg");
	Client client;
	int inzetRonde = 0;
	
	private JLabel smsg = new JLabel();
	private JPanel serverMessage = new JPanel();
	private JScrollPane jsp;
	
	public HoofdSchermGUI(Client cc)
	{	
		
		serverMessage.setBackground( new Color(247, 247, 189) );
		serverMessage.add(smsg);
		
		jsp = new JScrollPane(serverMessage, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		jsp.setMaximumSize(new Dimension(400, 50));
		jsp.setMinimumSize(new Dimension(400, 50));
		jsp.setPreferredSize(new Dimension(400, 50));
		
		this.speler = cc.speler;
		client = cc;
		
		cc.setHoofdScherm(this);
		menu = new MenuGUI(this);
		tafel = new PokerTafelGUI(this);
		
		jf = new JFrame();
		
		Container c = jf.getContentPane();
		
		c.add(menu.show(), BorderLayout.NORTH);
		c.add(tafel, BorderLayout.CENTER);
		c.add(jsp, BorderLayout.SOUTH);
		
		
		java.awt.Dimension ss = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ( (ss.getWidth() / 2 ) - 150 );
		int y = (int) ( (ss.getHeight() / 2 ) - 150 );
		
		jf.setLocation( x, y );
		
		jf.setSize(800, 600);
		
		jf.setResizable(false);
		jf.setTitle("Poker - hoofdScherm");
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.validate();
	}
	
	public void ikhebdata()
	{
		while(tafel == null){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {}
		}


		if(client.spel != null){
			spelronde = client.spel.sr;
			tafel.kaarten.krt = client.spel.sr.tafel;
			smsg.setText(client.spel.serverMessage + "</html>");
			jsp.getVerticalScrollBar().setValue( jsp.getVerticalScrollBar().getMaximum() );
			jsp.validate();
			serverMessage.validate();
		}else{
			
		}
		
		// Alles wat het scherm moet tonen updaten!
		Deelname d;
		for(ListIterator i = client.spel.sr.deelnames.listIterator(); i.hasNext(); )
		{
			d = (Deelname) i.next();
			
			if( tafel != null && d != null)
				tafel.setGegevens(d.speler.spelerId, d.speler.getSaldo(), d.speler.getNaam());
			
			if(d.kaart.size() == 0)
			{
				tafel.updateFold(d.speler.spelerId);
			}
			if(d.removeCards)
				tafel.removeAllCards();
		}
		if(client.spel.sr.lastActionSpeler != 0)
		{	
			
			tafel.setLastAction(client.spel.sr.lastActionSpeler);
			
		}
		
		validate();
		repaint();
		
		if(client.deelname == null)
		{
			
		}
		else
		{
			if(client.deelname.isAanBeurt && client.deelname.hasfolded == false)
			{
				if(client.deelname.smallBlind)
				{
					this.smallBlind();
					client.deelname.smallBlind = false;
				} else if (client.deelname.bigBlind)
				{
					this.bigBlind();
					client.deelname.bigBlind = false;
				} else {
					if(client.spel.sr.inzetRonde == 2)
					{
						tafel.showButtons(PokerTafelGUI.FOLD_CALL_OPTION);
					}
					else if(client.spel.sr.inzetRonde < 2)
					{
						tafel.showButtons(PokerTafelGUI.FOLD_CHECK_BET_OPTION);
					}
				}
			} 
			else {
				tafel.showButtons(PokerTafelGUI.NO_OPTION);
			}
		}
		validate();
		repaint();
	}
	
	public void showHostGameMenu()
	{
		JFrame hostG = new JFrame();
		Container c = hostG.getContentPane();
		
		JPanel hg = new JPanel();
		hg.setLayout(new GridLayout(7,1));
		
		JPanel t;
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.setPreferredSize( new Dimension(1, 5) );
		
		hg.add(t);
				
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.add( Box.createRigidArea( new Dimension(20, 1) ) );
		t.add( new JLabel("Game name") );
		t.add( Box.createRigidArea( new Dimension(20, 1) ) );
		JTextField name = new JTextField(20 /* game naam uit settings */);
		t.add(name);
		t.add( Box.createRigidArea( new Dimension(20, 1) ) );
		
		hg.add(t);
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.setPreferredSize( new Dimension(1, 5) );
		
		hg.add(t);
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.add( Box.createRigidArea( new Dimension(20, 1) ) );
		t.add( new JLabel("Port") );
		t.add( Box.createRigidArea( new Dimension(55, 1) ) );
		JTextField port = new JTextField(4 /* poort uit settings */);
		t.add(port);
		t.add( Box.createRigidArea( new Dimension(60, 1) ) );
		
		hg.add(t);
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.setPreferredSize( new Dimension(1, 5) );
		
		hg.add(t);
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		HostGameListener hgl = new HostGameListener(hostG);
		t.add( Box.createRigidArea( new Dimension(20, 1) ) );
		
		JButton host = new JButton("Host");
		t.add(host);
		host.addActionListener(hgl);
		
		JButton cancel = new JButton("Cancel");
		t.add(cancel);
		cancel.addActionListener(hgl);
		
		hg.add(t);
		
		c.add(hg);
		java.awt.Dimension ss = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ( (ss.getWidth() / 2 ) - 150 );
		int y = (int) ( (ss.getHeight() / 2 ) - 150 );
		
		hostG.setLocation( x, y );
		
		hostG.setSize(200, 170);
		hostG.setResizable(false);
		hostG.setTitle("Host a game");
		hostG.setVisible(true);
		hostG.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		hostG.validate();
	}
	
	public void showJoinGameMenu()
	{
		JFrame joinG = new JFrame();
		Container c = joinG.getContentPane();
		
		JPanel jg = new JPanel();
		jg.add( new JLabel("Enter IP-Address") );
		ip = new JTextField(15);
		jg.add(ip);
		
		JButton join = new JButton("Join");
		jg.add(join);
		join.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{	
				if(ip.getText().matches("[0-9]{2,3}?\\.?[0-9]{2,3}?\\.?[0-9]{2,3}?\\.?[0-9]{2,3}"))
				{
					//System.out.println("CONNECT"); /* CONNECT - ben niet zo gek om dit ook nog te maken ;) */
				}
				else
				{
					JOptionPane.showMessageDialog(null, "this is not a valid ip");
				}
			}
		});
		
		c.add(jg);
		java.awt.Dimension ss = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ( (ss.getWidth() / 2 ) - 150 );
		int y = (int) ( (ss.getHeight() / 2 ) - 150 );
		
		joinG.setLocation( x, y );
		
		joinG.setSize(250, 90);
		joinG.setResizable(false);
		joinG.setTitle("Host a game");
		joinG.setVisible(true);
		joinG.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		joinG.validate();
	}
	
	public void showSettingsMenu()
	{	
		new SettingsGUI(this);
	}
	
	public void check()
	{
		spelronde.check(speler);
		client.interrupt();
	}
	
	public void call(int inzet)
	{
		//System.out.println("HoofdScherm.call(" + inzet + ")");
		spelronde.call(speler, inzet);
		client.interrupt();
	}
	
	public void smallBlind()
	{
		spelronde.smallBlind(speler);
		client.interrupt();
	}
	
	public void bigBlind()
	{
		spelronde.bigBlind(speler);
		client.interrupt();
	}
	
	public void bet(int inzet)
	{
		spelronde.bet(speler, inzet);
		client.interrupt();
	}
	
	public void raise(int inzet)
	{
		spelronde.raise(speler, inzet);
		client.interrupt();
	}
	
	public void fold()
	{
		spelronde.fold(speler);
		client.interrupt();
	}
	
	public void allIn()
	{
		spelronde.allIn(speler);
		client.interrupt();
	}
	

	
	public void stop()
	{
		spelronde.stop();			//spelronde is nog niet te gebruiken
		client.interrupt();
	}
	
	private class HostGameListener implements ActionListener {
		private JFrame hg;
		public HostGameListener(JFrame hg)
		{
			this.hg = hg;
		}
		
		public void actionPerformed(ActionEvent e)
		{
			if(e.getActionCommand() == "Host")
			{
				//System.out.println("Host Game");
			}
			
			else {
				hg.dispose();
			}
		}
		
	};
}